#-*- coding: utf-8 -*-
#===============================================================================
# IMPORTS
#===============================================================================
from __future__ import unicode_literals

from django.contrib.auth.models import User, AbstractBaseUser, AbstractUser, BaseUserManager
from django.db import models
from apiManager.models import *

class ApiAccountManager(models.Manager):
    def create_api_account(self, accountName, keyID, vCode):
        account = ApiAccount()
        account.accountName = accountName
        account.keyID = keyID
        account.vCode = vCode
#         account = self.create_api_account(accountName=accountName, keyID=keyID, vCode=vCode)
        account.save()
        return account
    
    def list_users(self):
        return ApiUser.objects.filter(apiAccount_id = self.accoutnID)

    def list_admin(self):
        return ApiUserManager().filter(apiAccount_id = self.accoutnID, is_api_admin=True)


class ApiAccount(models.Model):
    accountID = models.AutoField(primary_key = True)
    accountName = models.CharField(max_length=200, unique=True, db_index=True )
    accountType = models.CharField(max_length=200, null=True)
    keyID = models.IntegerField(unique=True, db_index=True)
    vCode = models.CharField(max_length=400)
     
    objects = ApiAccountManager()
#     
    def __unicode__(self):
        return self.accountName
             
#     def users_list(self):
#         users = MyUser.objects.filter(apiAccount_id = self.accountID)
#         return users
        
#     def admin_user(self):
#         admin = MyUser.objects.filter(apiAccount_id = self.accountID, is_api_admin = True)
#         return admin

class ApiUserManager(BaseUserManager):
    def create_api_user(self, username, firstname, lastname, email, password, is_staff, is_superuser, is_api_admin, account):
        apiuser = self.create_api_user(username = username, firstname=firstname, lastname=lastname, email=email, password=password, is_staff=is_staff, is_superuser=is_superuser, is_api_admin=is_api_admin, account=account)
        return apiuser
    
    def create_superuser(self, username, firstname, lastname, email, password, account):
        admin_user = self.create_api_user(username=username, firstname=firstname, lastname=lastname, email=email, password=password, is_staff=True, is_superuser=True, is_api_admin=True, account=account)
        return admin_user
        
class ApiUser(AbstractUser):
    is_api_admin = models.BooleanField(default=False)
    #is_producer = models.BooleanField(default=False)
    #gérer production list => accès, etc...
    apiAccount = models.ForeignKey(ApiAccount)
     
    objects = ApiUserManager()
 
     
    def __unicode__(self):
        username = self.get_username()
        return username